package gt.controller.waybill;

import gt.common.CarriersConstant;
import gt.entity.base.DataMessage;
import gt.entity.carrier.Carrier;
import gt.entity.token.Token;
import gt.entity.waybill.Waybill;
import gt.service.token.TokenService;
import gt.service.waybill.WaybillService;
import gt.service.waybillDesc.WaybillDescService;
import gt.util.wx.TemplateUtil;
import gt.util.wx.ToolSpring;

import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.gt.service.AgentKdapiService;

/**
 * @author wenyu
 * @date 2017年7月26日 上午11:18:10
 */

public class QueryWaybill{

	// 快递查询线程池
	static Executor waybill_ex = Executors.newCachedThreadPool();
	
	/**
	 * 快递信息查询方法
	 * @param expressCode  快递单号
	 * @param waybillService  快递基本信息服务类
	 * @param waybillDescService 快递详情服务类
	 * @param openId 微信用户编号
	 * @return
	 * @author liuwy
	 */
	public DataMessage search(String expressCode, String openId){
		
	 	DataMessage dm = new DataMessage();
		if (expressCode == null || expressCode == "") {
			dm.setResult(1);
			return dm;
		}
		//调用快递查询接口，查快递信息
			AgentKdapiService agentKdapiService = (AgentKdapiService)ToolSpring.getBean("agentKdapiService");
			TokenService tokenService = (TokenService)ToolSpring.getBean("tokenService");
			String result = agentKdapiService.query(expressCode);
			
			
			JSONObject json = JSONObject.fromObject(result);
			// 物流状态
			int status = Integer.parseInt(json.getString("status"));
			// 物流信息
			String data = json.getString("data");
			// 存放快递公司信息
			Carrier carrier = new Carrier();
			if (json.containsKey("carrierCode")) {
				carrier.setCarrierCode(json.getString("carrierCode"));
				List<Carrier> allc = CarriersConstant.ALL_CARRIERS2;
				if (!allc.isEmpty()) {
					for (Carrier c : allc) {
						if (c.getCarrierCode().equals(carrier.getCarrierCode())) {
							carrier.setCarrierId(c.getCarrierId());
							carrier.setCarrierName(c.getCarrierName());
							break;
						}
					}
				}
			}
			// 查不到数据的情况下返回result为1
			if (data.equals("[]")) {
				dm.setResult(1);
			} else {
				dm.setResult(0);
			}
			dm.setMessage(null);
			dm.setData(data);
			// 线程执行增加删除修改
			Waybill add = new Waybill();
			add.setUserId(0L);
			add.setServiceStatus(status); // 物流状态
			add.setServiceStatusDesc(data);// 物流信息
			add.setCarrierId(carrier.getCarrierId());// 快递公司id
			add.setCarrierName(carrier.getCarrierName());// 快递公司名称
			add.setCarrierCode(carrier.getCarrierCode());// 快递公司code
			add.setThirdId(openId);// 微信查询人id
			add.setWaybillType(3);// 类型
			add.setExpressCode(expressCode);// 快递单号
			// 调用线程池执行增加，需改操作
			WaybillService waybillService = (WaybillService)ToolSpring.getBean("waybillService");
			WaybillDescService waybillDescService = (WaybillDescService)ToolSpring.getBean("waybillDescService");
			try {
				waybill_ex.execute(new WaybillRunnable(add, waybillService,
						waybillDescService));
			} catch (Exception e) {
				e.printStackTrace();
			}
			//发送模板消息
			JSONArray waybillDescArray = JSONArray.fromObject(data);
			String firstDesc = null;
			if(status >1){
				
				firstDesc = JSONObject.fromObject(waybillDescArray.get(0)).getString("time")+"   "+JSONObject.fromObject(waybillDescArray.get(0)).getString("content");
			}else{
				firstDesc = "您的运单暂时没有最新物流信息";
			}
			String jsonMsg = TemplateUtil.makeRouteMessage(openId, "RWBRpvI3QDhleVyvqe7OMgfxO7sDQWAQS1D2NFBM9Zc", "http://liuwy.tunnel.qydev.com/wechat/waybill/toSearchDetails.action?expressCode="+expressCode, "#ff6600", carrier.getCarrierName(), expressCode, firstDesc+"");
			Token t = tokenService.queryById(1L);
			TemplateUtil.sendTemplateMessage(t.getWxKey(), jsonMsg);
			return dm;
	}

/**
 * 测试主函数
 * @param args
 */
	 public static void main(String[] args) {
		 
	}
	 
}